package controllers

import (
	"fmt"
	"github.com/beego/beego/v2/server/web"
	"math/rand/v2"
	"neutron_probe/services"
	"time"
)

// Operations about Statistic
type TestSoapController struct {
	web.Controller
}

// @Title getObjById
// @Description 根据Id获取中子仪统计
// @Param	body		body 	string	true		"body for statistic content"
// @Success 200 {int} models.Statistic.Id
// @Failure 403 body is empty
// @router /getObjById [post]
func (this *TestSoapController) SoapGetData() {
	//我要beego返回soap的响应
	/* 返回的内容
	<?xml version="1.0" encoding="utf-8"?>
	<DataSet xmlns="http://localhost/">
	  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
	    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
	      <xs:complexType>
	        <xs:choice minOccurs="0" maxOccurs="unbounded">
	          <xs:element name="Table1">
	            <xs:complexType>
	              <xs:sequence>
	                <xs:element name="item" type="xs:string" minOccurs="0" />
	                <xs:element name="value" type="xs:string" minOccurs="0" />
	              </xs:sequence>
	            </xs:complexType>
	          </xs:element>
	        </xs:choice>
	      </xs:complexType>
	    </xs:element>
	  </xs:schema>
	  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
	    <NewDataSet xmlns="">
	      <Table1 diffgr:id="Table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
	        <item>DataTime</item>
	        <value>2023-12-14 15:54:04</value>
	      </Table1>
	      <Table1 diffgr:id="Table12" msdata:rowOrder="1" diffgr:hasChanges="inserted">
	        <item>Speed</item>
	        <value>2</value>
	      </Table1>
	      <Table1 diffgr:id="Table13" msdata:rowOrder="2" diffgr:hasChanges="inserted">
	        <item>Flux</item>
	        <value>2250</value>
	      </Table1>
	      <Table1 diffgr:id="Table14" msdata:rowOrder="3" diffgr:hasChanges="inserted">
	        <item>Load</item>
	        <value>312.5</value>
	      </Table1>
	      <Table1 diffgr:id="Table15" msdata:rowOrder="4" diffgr:hasChanges="inserted">
	        <item>SiO2</item>
	        <value>1.16</value>
	      </Table1>
	      <Table1 diffgr:id="Table16" msdata:rowOrder="5" diffgr:hasChanges="inserted">
	        <item>Al2O3</item>
	        <value>1.86</value>
	      </Table1>
	      <Table1 diffgr:id="Table17" msdata:rowOrder="6" diffgr:hasChanges="inserted">
	        <item>Fe2O3</item>
	        <value>0</value>
	      </Table1>
	      <Table1 diffgr:id="Table18" msdata:rowOrder="7" diffgr:hasChanges="inserted">
	        <item>CaO</item>
	        <value>55.8</value>
	      </Table1>
	      <Table1 diffgr:id="Table19" msdata:rowOrder="8" diffgr:hasChanges="inserted">
	        <item>MgO</item>
	        <value>0</value>
	      </Table1>
	      <Table1 diffgr:id="Table110" msdata:rowOrder="9" diffgr:hasChanges="inserted">
	        <item>K2O</item>
	        <value>0.56</value>
	      </Table1>
	      <Table1 diffgr:id="Table111" msdata:rowOrder="10" diffgr:hasChanges="inserted">
	        <item>Na2O</item>
	        <value>0</value>
	      </Table1>
	      <Table1 diffgr:id="Table112" msdata:rowOrder="11" diffgr:hasChanges="inserted">
	        <item>SO3</item>
	        <value>0.95</value>
	      </Table1>
	    </NewDataSet>
	  </diffgr:diffgram>
	</DataSet>
	*/
	xmlContext := `<?xml version="1.0" encoding="utf-8"?>
	<DataSet xmlns="http://localhost/">
	  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
	    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
	      <xs:complexType>
	        <xs:choice minOccurs="0" maxOccurs="unbounded">
	          <xs:element name="Table1">
	            <xs:complexType>
	              <xs:sequence>
	                <xs:element name="item" type="xs:string" minOccurs="0" />
	                <xs:element name="value" type="xs:string" minOccurs="0" />
	              </xs:sequence>
	            </xs:complexType>
	          </xs:element>
	        </xs:choice>
	      </xs:complexType>
	    </xs:element>
	  </xs:schema>
	  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
	    <NewDataSet xmlns="">
	      <Table1 diffgr:id="Table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
	        <item>DataTime</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table12" msdata:rowOrder="1" diffgr:hasChanges="inserted">
	        <item>Speed</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table13" msdata:rowOrder="2" diffgr:hasChanges="inserted">
	        <item>Flux</item>
	        <value>2250</value>
	      </Table1>
	      <Table1 diffgr:id="Table14" msdata:rowOrder="3" diffgr:hasChanges="inserted">
	        <item>Load</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table15" msdata:rowOrder="4" diffgr:hasChanges="inserted">
	        <item>SiO2</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table16" msdata:rowOrder="5" diffgr:hasChanges="inserted">
	        <item>Al2O3</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table17" msdata:rowOrder="6" diffgr:hasChanges="inserted">
	        <item>Fe2O3</item>
	        <value>0</value>
	      </Table1>
	      <Table1 diffgr:id="Table18" msdata:rowOrder="7" diffgr:hasChanges="inserted">
	        <item>CaO</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table19" msdata:rowOrder="8" diffgr:hasChanges="inserted">
	        <item>MgO</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table110" msdata:rowOrder="9" diffgr:hasChanges="inserted">
	        <item>K2O</item>
	        <value>%s</value>
	      </Table1>
	      <Table1 diffgr:id="Table111" msdata:rowOrder="10" diffgr:hasChanges="inserted">
	        <item>Na2O</item>
	        <value>0</value>
	      </Table1>
	      <Table1 diffgr:id="Table112" msdata:rowOrder="11" diffgr:hasChanges="inserted">
	        <item>SO3</item>
	        <value>0.95</value>
	      </Table1>
	    </NewDataSet>
	  </diffgr:diffgram>
	</DataSet>`
	// 设置Content-Type为text/xml
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "text/xml; charset=utf-8")

	// 写入SOAP响应到HTTP响应体
	nowMinute := time.Now().Truncate(time.Minute)
	content := fmt.Sprintf(xmlContext,
		nowMinute.Format(time.DateTime),
		random(2, 0),        //Speed
		random(312.5, 0),    //Load
		random(1.16, 0.03),  //SiO2
		random(1.86, 0.03),  //Al2O3
		random(55.8, 3),     //cao
		random(0.1, 0.01),   //myo
		random(0.56, 0.005), //K2O
	)
	this.Ctx.ResponseWriter.Write([]byte(content))
}

func random(center float64, dot float64) string {
	//生成以center为中心,以dot为变化半径的随机数
	//返回0,1之间的随机数
	value := rand.Float64()*dot*2 + center
	return fmt.Sprintf("%f", value)
}
func (this *TestSoapController) TestSoapGetData() {
	services.GetFetchDevice4Dfmc().FetchDataRepeatedly()
	customFormat := "2006-01-02 15:04:05"
	this.Ctx.Resp(fmt.Sprint("TestSoapGetData ok,now is %t", time.Now().Format(customFormat)))
}
func init() {
	web.Router("/MyService.asmx/GetData", &TestSoapController{}, "get,post:SoapGetData")
	web.Router("/MyService.asmx/test", &TestSoapController{}, "get:TestSoapGetData")
}
